package com.ocm.tourism.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ocm.tourism.core.domain.Catalogues;
import com.ocm.tourism.core.service.CataloguesService;
import com.ocm.tourism.core.service.NewsLayerService;
import com.ocm.tourism.webclient.model.WebClientModel;

@RequestMapping("/news")
@Controller
public class NewsController {

   @Autowired
   private ApplicationContext appContext;

   @Autowired
   CataloguesService cataloguesService;

   @RequestMapping(value = "/view/{newType}")
   public String viewNew(@PathVariable("newType") String newType, Model uiModel) {
      Catalogues catalogues = cataloguesService.findCatalogues(newType);
      populateEditForm(uiModel, catalogues);
      uiModel.addAttribute("newType", newType);
      return "news/view";
   }

   @RequestMapping(value = "/view/{newType}/light")
   public String viewContentViaAjax(@PathVariable("newType") String newType, Model uiModel) {
      Catalogues catalogues = cataloguesService.findCatalogues(newType);
      populateEditForm(uiModel, catalogues);
      uiModel.addAttribute("newType", newType);
      return "news/view/light";
   }

   @RequestMapping(value = "/mainIntrodution")
   public String viewIntroduction(Model uiModel) {
      /*String newType = "mainIntrodution";
      Catalogues catalogues = cataloguesService.findCatalogues(newType);
      populateEditForm(uiModel, catalogues);*/
      uiModel.addAttribute("submenuSelected", "introdution");
      return "news/introdution";
   }

   @RequestMapping(value = "/update/{newType}", method = RequestMethod.GET)
   public String updateForm(@PathVariable("newType") String newType, Model uiModel) {
      Catalogues catalogues = cataloguesService.findCatalogues(newType);
      if (catalogues == null)
         catalogues = new Catalogues();
      populateEditForm(uiModel, catalogues);
      uiModel.addAttribute("submenuSelected", "introdution");
      uiModel.addAttribute("newType", newType);
      return "news/update";
   }

   @RequestMapping(value = "/otherResources", method = RequestMethod.GET)
   public String getOtherNews(Model uiModel) {
      List<WebClientModel> webClientModels = getNewsLayerService().getOtherNews();
      uiModel.addAttribute("webClientModels", webClientModels);
      return "news/otherResources";
   }

   @RequestMapping(method = RequestMethod.GET)
   public String getNews(Model uiModel) {
      return "news/welcome";
   }

   @RequestMapping(method = RequestMethod.POST, produces = "text/html")
   public String create(@Valid Catalogues catalogues, @RequestParam("newType") String newType, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
       if (bindingResult.hasErrors()) {
           populateEditForm(uiModel, catalogues);
           return "news/update";
       }
       uiModel.asMap().clear();
       catalogues.setCataloguesType(newType);
       cataloguesService.saveCatalogues(catalogues);
       return "redirect:/news/view/"+newType;
   }

   private void populateEditForm(Model uiModel, Catalogues catalogues) {
      uiModel.addAttribute("catalogues", catalogues);
  }
   
   public NewsLayerService getNewsLayerService(){
      NewsLayerService newsLayerService =  this.appContext.getBean(NewsLayerService.class);
      return newsLayerService;
   }
}

